'''
Desc: 嵌套布局示例
Author: 陈爱国
Date: 2024-09-09 22:32:07
'''
from PyQt5.QtWidgets import *

class NestedLayoutDemo(QWidget):
    def __init__(self):
        super(NestedLayoutDemo, self).__init__()
        self.initUi()

    def initUi(self):
        # self.show()
        # 设置窗口标题
        self.setWindowTitle("嵌套布局示例")
        # 设置窗口大小
        self.resize(1000, 600)

        # 创建全局布局
        layout = QHBoxLayout()

        # 创建局部布局
        hlayout = QHBoxLayout()
        vlayout = QVBoxLayout()
        flayout = QFormLayout()
        glayout = QGridLayout()

        # 创建控件
        one_btn = QPushButton(str(1))
        two_btn = QPushButton(str(2))
        three_btn = QPushButton(str(3))
        four_btn = QPushButton(str(4))
        five_btn = QPushButton(str(5))
        six_btn = QPushButton(str(6))
        seven_btn = QPushButton(str(7))
        eight_btn = QPushButton(str(8))
        # 向布局上添加控件
        hlayout.addWidget(one_btn)
        hlayout.addWidget(two_btn)
        vlayout.addWidget(three_btn)
        vlayout.addWidget(four_btn)
        flayout.addRow(QLabel("输入姓名"), QLineEdit())
        flayout.addRow(QLabel("输入密码"), QLineEdit())
        glayout.addWidget(five_btn, 0, 0)
        glayout.addWidget(six_btn, 0, 1)
        glayout.addWidget(seven_btn, 1, 0)
        glayout.addWidget(eight_btn, 1, 1)

        # 创建容器控件
        hwidget = QWidget()
        vwidget = QWidget()
        fwidget = QWidget()
        gwidget = QWidget()

        # 设置容器控件的布局
        hwidget.setLayout(hlayout)
        vwidget.setLayout(vlayout)
        fwidget.setLayout(flayout)
        gwidget.setLayout(glayout)

        # 将容器控件添加到全局布局
        layout.addWidget(hwidget)
        layout.addWidget(vwidget)
        layout.addWidget(fwidget)
        layout.addWidget(gwidget)

        # 设置窗口为全局布局
        self.setLayout(layout)
